package page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * PageAnalyzer class analyzes the collected pages content 
 * 
 * @version February 6, 2012
 * @author Jay Fernandez
 * @author Kyle Richardson
 */
public class PageAnalyzer {
	/** Map of keywords and their count. */
	Map<String, Integer> keyword_count;
	
	/** Set of keywords to keep track of. */
	List<String> keywords;
	
	/** Count of pages retrieved. */
	int page_count;
	
	/** Count of all words. */
	int word_count;
	
	/** Count of URLs. */
	int URL_count;
	
	/**
	 * Constructor that initializes with everything
	 * empty and values set to 0.
	 */
	public PageAnalyzer() {
		keyword_count = new HashMap<String, Integer>();
		keywords = new ArrayList<String>();
		page_count = 0;
		word_count = 0;
		URL_count = 0;
	}
	
	/**
	 * Adds the given word to the set of keywords.
	 * 
	 * @param keyword The word to be added.
	 */
	public void addKeyword(String keyword) {
		keywords.add(keyword);
		keyword_count.put(keyword, 0);
	}
	
	/**
	 * Increases the count for each keyword that
	 * is found within the given list.
	 * 
	 * @param text The text to search through.
	 */
	public void findKeywords(List<String> text) {
		String keyword;
		for (int i = 0; i < text.size(); i++) {
			if (keywords.contains(text.get(i))) {
				keyword = text.get(i);
				keyword_count.put(keyword, keyword_count.get(keyword) + 1);
			}
		}
		word_count = word_count + text.size();
		page_count++;
	}
	
	/**
	 * Counts the number of URLs and adds it
	 * to the total of URLs retrieved.
	 * 
	 * @param URLs List of URLs.
	 */
	public void addURLs(List<String> URLs) {
		this.URL_count = this.URL_count + URLs.size();
	}
	
	/**
	 * @return the list of keywords.
	 */
	public List<String> getKeywords() {
		return keywords;
	}
	
	/**
	 * @return the total number of pages crawled.
	 */
	public int getPagesRetrieved() {
		return page_count;
	}
	
	/**
	 * @return the average amount of words per page.
	 */
	public int getAvgWords() {
		if (page_count != 0) {
			return word_count / page_count;
		}
		return 0;
	}
	
	/**
	 * @return the average amount of URLs per page.
	 */
	public int getAvgURLs() {
		if (page_count != 0) {
			return URL_count / page_count;
		}
		return 0;
	}
	
	/**
	 * @param keyword The keyword to count.
	 * @return the total hits a keyword has.
	 */
	public int getTotalHits(String keyword) {
		return keyword_count.get(keyword);
	}
	
	/**
	 * @param keyword The keyword to count.
	 * @return the average hits per page a keyword has.
	 */
	public double getAvgHits(String keyword) {
		if(page_count != 0) {
			return (double) keyword_count.get(keyword) / (double) page_count;
		}
		return 0;
	}
}
